import {distance, point} from '@turf/turf'

minemap.domainUrl = '//minedata.cn'
minemap.spriteUrl = '//minedata.cn/minemapapi/v1.3/sprite/sprite'
minemap.serviceUrl = '//minedata.cn/service'
minemap.accessToken = '5fa8b08a69b347b99bf0e2722582c5e8'
minemap.solution = 3590
minemap.dataDomainUrl = '//datahive.minedata.cn';
var map = null;
//变道提醒
var showEvent = [[136, 155], [236, 255], [228, 240], [328, 340]];

var BianDaoIcon = document.querySelector("#biandao-icon");


var pos = [116.43610471641222, 39.94948604452273]
var pos1 = [116.4377999930644, 39.95969006169207]

var to = point(pos);
var to1 = point(pos1);

minemap.TRW = {
    setProgress: function (seq) {
        var zoom = map.getZoom();
        if (zoom <= 17) return;

        var from = point(map.getCenter().toArray());
        var options = {units: 'kilometers'};
        var dist = distance(from, to, options);
        var dist1 = distance(from, to1, options);

        // var dist = 1.0;
        // var dist1 = 1.0;

        if (zoom > 17 &&
            (
                (dist < 1 &&
                    ((seq > showEvent[0][0] && seq < showEvent[0][1]) ||
                        (seq > showEvent[1][0] && seq < showEvent[1][1]))
                ) ||
                (dist1 < 1 &&
                    ((seq > showEvent[2][0] && seq < showEvent[2][1]) ||
                        (seq > showEvent[3][0] && seq < showEvent[3][1])))
            )
        ) {


            var bounds = map.getBounds();

            var east = bounds.getEast();
            var west = bounds.getWest();
            var north = bounds.getNorth();
            var south = bounds.getSouth();

            if (pos[0] > west && pos[0] < east && pos[1] > south && pos[1] < north) {
                if (map.getLayoutProperty('biandaoMarker', 'visibility') !== 'visible') {
                    map.setLayoutProperty('biandaoMarker', 'visibility', 'visible');
                }
                BianDaoIcon.style.display = 'block';
            }

            if (pos1[0] > west && pos1[0] < east && pos1[1] > south && pos1[1] < north) {
                if (map.getLayoutProperty('biandaoMarker', 'visibility') !== 'visible') {
                    map.setLayoutProperty('biandaoMarker', 'visibility', 'visible');
                }
                BianDaoIcon.style.display = 'block';
            }

        } else {
            if (map.getLayoutProperty('biandaoMarker', 'visibility') !== 'none') {
                map.setLayoutProperty('biandaoMarker', 'visibility', 'none');
            }

            BianDaoIcon.style.display = 'none';
        }
    },
    request: function () {
    },
    replace: function () {
    },
    complex3dSeq: []
}

//全局飞行状态，是否在飞行
var flyStatus = false;

setTimeout(function () {
    map.repaint = true;
},1000 * 5);

export function initMap() {
    map = new minemap.Map({
        container: 'map',
        style: '//minedata.cn/service/solu/style/id/5571',
        center: [116.43780661348444, 39.95854775210401],
        zoom: 13,
        pitch: 49,
        bearing: 37.6,
        maxZoom: 17.9
    });
    map.addControl(new minemap.Scale(), 'bottom-left');

    return map;
}